//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/MongoImpl.java $
//$Id: MongoImpl.java 117 2013-03-30 23:27:57Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.Database;
import de.piratech.dasding.data.database.EventCategoryData;
import de.piratech.dasding.data.database.EventData;
import de.piratech.dasding.data.database.LocationData;
import de.piratech.dasding.data.database.UserData;
import java.net.UnknownHostException;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 */
@Service
public class MongoImpl implements Database {

  private static final Logger LOG = LoggerFactory.getLogger(MongoImpl.class);
  private Mongo mMongo;
  @Value("${database.url}")
  private String mUrl;
  @Value("${database.user}")
  private String mUsername;
  @Value("${database.password}")
  private String mPassword;
  @Value("${database.database}")
  private String mDatabase;

  @PostConstruct
  private void init() throws UnknownHostException {
    LOG.debug("database {}", new Object[] { mDatabase });
    mMongo = StringUtils.isBlank(mUrl) ? new Mongo() : new Mongo(mUrl);
    mMongo.setWriteConcern(WriteConcern.SAFE);
  }

  private DB getDB() {
    DB ret = mMongo.getDB(mDatabase);
    if (StringUtils.isNotBlank(mUsername)) {
      ret.authenticate(mUsername, mPassword.toCharArray());
    }
    return ret;
  }

  @Override
  public UserData getUserData() {
    return new UserDataImpl(getDB());
  }

  @Override
  public EventData getEventData() {
    return new EventDataImpl(getDB());
  }

  @Override
  public ActionData getActionData() {
    return new ActionDataImpl(getDB());
  }

  @Override
  public ActionCategoryData getActionCategoryData() {
    return new ActionCategoryDataImpl(getDB());
  }

  @Override
  public EventCategoryData getEventCategoryData() {
    return new EventCategoryDataImpl(getDB());
  }

  public LocationData getLocationData() {
    return new LocationDataImpl(getDB());
  }
}
